package com.SuanZhengYi.Match;

import java.util.Arrays;

public class ErFenChaZhao {
    public static void main(String[] args) {
        int[]arr={7,23,79,81,103,127,131,147};
        System.out.println(binarySearch(arr, 81));
        System.out.println(Arrays.binarySearch(arr, 81));
    }
    public static int binarySearch(int[]arr,int data){
        int left=0;
        int right=arr.length-1;
        while (left<=right){
            int midIndex=(left+right)/2;
            if(arr[midIndex]>data){
                right=midIndex-1;
            }else if (arr[midIndex]<data){
                left=midIndex+1;
            }else {
                return midIndex;
            }
        }
        return -1;
    }
}
